package com.wangch.sqlsession;

import com.wangch.pojo.Configuration;
import com.wangch.pojo.MappedStatement;

import java.lang.reflect.*;
import java.util.List;

public class DefaultSqlSession implements SqlSession {
    private Configuration configuration;
    public DefaultSqlSession (Configuration configuration){
        this.configuration = configuration;
    }
    public <E> List<E> selectList(String statementId, Object... params) throws Exception{
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        List<Object> query = new SimpleExecutor().query(configuration, mappedStatement, params);
        return (List<E>) query;
    }

    public <T> T selectOne(String statementId, Object... params) throws Exception{
        List<Object> objects = selectList(statementId, params);
        if(objects.size() == 1){
            return (T) objects.get(0);
        }else{
            throw new RuntimeException("查询结果为空或者返回结果过多");
        }
    }

    private void update(String statementId, Object... params) throws Exception{
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        new SimpleExecutor().update(configuration, mappedStatement, params);
    }

    private void insert(String statementId, Object... params) throws Exception{
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        new SimpleExecutor().insert(configuration, mappedStatement, params);
    }

    private void delete(String statementId, Object... params) throws Exception{
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        new SimpleExecutor().delete(configuration, mappedStatement, params);
    }

    public <T> T getMapper(final Class<?> mapperClass) {
        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                String methodName = method.getName();
                String className = method.getDeclaringClass().getName();
                String statementId = className + "." + methodName;
                if (methodName.contains("find")) {
                    Type genericReturnType = method.getGenericReturnType();
                    if (genericReturnType instanceof ParameterizedType) {
                        List<Object> objects = selectList(statementId, args);
                        return objects;
                    }
                    return selectOne(statementId, args);
                }
                if(methodName.contains("insert")){
                    insert(statementId, args);
                }
                if(methodName.contains("update")){
                    update(statementId, args);
                }
                if (methodName.contains("delete")){
                    delete(statementId, args);
                }
                return null;
            }
        });
        return (T) proxyInstance;
    }




}
